#1.添加cmake版本说明
cmake_minimum_required(VERSION 3.0.0)
#2.声明工程名称
project(gateway_server)
set(target "gateway_server")

# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

#############################################################################################################
#3.检测并生成proto框架代码
#1，添加所需的proto映射代码文件名称
set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
set(proto_files base.proto user.proto file.proto message.proto transmite.proto friend.proto gateway.proto notify.proto speech.proto)
#2，检测框架代码文件是否已经生成
set(proto_hxx "")
set(proto_cc "")
set(proto_srcs "")
foreach(proto_file ${proto_files})
#3，如果没有生成，则预定义生成指令--用于在构建项目之间先生成框架代码
string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file})
string(REPLACE ".proto" ".pb.h" proto_hh ${proto_file})
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${proto_cc})
add_custom_command(
    PRE_BUILD
    COMMAND protoc
    ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} -I ${proto_path} ${proto_path}/${proto_file}
    DEPENDS ${proto_path}/${proto_file}
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
    COMMENT "生成protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc})

endif()
#4.将所有生成的框架源码文件名称保存起来student-proto.cxxclasses-proto.cxx
list(APPEND proto_srcs ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc})
endforeach()
#############################################################################################################


# set(test_client "message_client")
# set(test_files "")
# aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/test test_files)
# add_executable(${test_client} ${test_files} ${proto_srcs} ${odb_srcs} )
# target_link_libraries(${test_client}  -lgflags 
#                                 -lspdlog -lfmt -lbrpc -lssl -lcrypto 
#                                 -lprotobuf -lleveldb -letcd-cpp-api 
#                                 -lodb-mysql -lodb -lodb-boost
#                                 -lhiredis -lredis++
#                                 -lcpprest -lcurl
#                                 -lpthread -lboost_system)
#############################################################################################################

#4，获取源码目录下的所有源码文件
set(src_files "")
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source src_files)
add_executable(${target} ${src_files} ${proto_srcs} ${odb_srcs})

#5，声明目标及依赖


#6，设置头文件默认搜索路径
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third/include)
#7.设置需要连接的库
target_link_libraries(${target} -lgflags 
                                -lspdlog -lfmt -lbrpc -lssl -lcrypto 
                                -lprotobuf -lleveldb -letcd-cpp-api 
                                -lodb-mysql -lodb -lodb-boost
                                -lhiredis -lredis++
                                -lcpprest -lcurl
                                -lpthread -lboost_system)
#8 设置安装路径
INSTALL(TARGETS ${target} RUNTIME DESTINATION bin)
